package com.example.wwk.graduationpractice.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.wwk.graduationpractice.Activity.AnswerActivity;
import com.example.wwk.graduationpractice.Activity.ChatConsultActivity;
import com.example.wwk.graduationpractice.Activity.LoginActivity;
import com.example.wwk.graduationpractice.Bean.AnswerBean;
import com.example.wwk.graduationpractice.Bean.PsychologistBean;
import com.example.wwk.graduationpractice.Bean.QuestionBean;
import com.example.wwk.graduationpractice.R;

import java.util.List;

/**
 * Created by wwk on 2017/4/18.
 */

public class AnswerAdapter extends RecyclerView.Adapter<AnswerAdapter.ViewHolder> {
    private List<AnswerBean> answerBeanList;
    private Context mcontext;
    private String thisuserName;
    private int identity;

    static class ViewHolder extends RecyclerView.ViewHolder {
        CardView cardView;
        TextView name;
        TextView time;
        TextView content;
        public ViewHolder(View itemView) {
            super(itemView);
            cardView = (CardView) itemView;
            name = (TextView) itemView.findViewById(R.id.answer_item_name);
            time = (TextView) itemView.findViewById(R.id.answer_item_time);
            content = (TextView) itemView.findViewById(R.id.answer_item_content);

        }
    }

    public AnswerAdapter(List<AnswerBean> mList, String thisusername, int identity2) {
        answerBeanList = mList;
        thisuserName = thisusername;
        identity = identity2;
    }

    @Override
    public AnswerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mcontext == null) {
            mcontext = parent.getContext();
        }
        View view;
        if (identity == LoginActivity.USER) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.answer_item, parent, false);
            final AnswerAdapter.ViewHolder holder = new AnswerAdapter.ViewHolder(view);
            holder.cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = holder.getAdapterPosition();
                    AnswerBean answerBean = answerBeanList.get(position);
                    Intent intent = new Intent(mcontext, ChatConsultActivity.class);
                    intent.putExtra("userName", answerBean.getName());         //获取该item咨询师的用户名
                    intent.putExtra("thisuserName", thisuserName);               //当前用户的用户名
                    mcontext.startActivity(intent);
                }
            });
            return holder;
        }
        else{
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.answer_item2, parent, false);
            AnswerAdapter.ViewHolder holder = new AnswerAdapter.ViewHolder(view);
            return holder;
        }

    }

    @Override
    public void onBindViewHolder(AnswerAdapter.ViewHolder holder, int position) {
        AnswerBean answerBean = answerBeanList.get(position);
        holder.name.setText(answerBean.getName());
        holder.time.setText(answerBean.getTime());
        holder.content.setText(answerBean.getContent());
    }
    @Override
    public int getItemCount() {
        return answerBeanList.size();
    }

}
